﻿using ComicBookApp.Data.DataAdapters.Adapters;
using ComicBookApp.Data.DataAdapters.Interfaces;
using ComicBookApp.DataModels;
using ComicBookApp.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ComicBookApp.Web.Controllers
{
    public class CreatorController : Controller
    {
        IComicBookAppAdapter _adapter;

        public CreatorController()
        {
            _adapter = new ComicBookAppAdapter();
        }
        public CreatorController(ComicBookAppAdapter adapter)
        {
            _adapter = adapter;
        }
        // GET: Creator
        public ActionResult Index()
        {
            List<Creator> creators = _adapter.GetAllCreators();
            return View(creators);
        }
        public ActionResult Add(int id = 0)
        {
            ComicBook comic = _adapter.GetComicBook(id);

            return View(comic);
        }
        [HttpPost]
        public ActionResult Add(int comicBookId, string name, string role)
        {
            Creator creator = new Creator(name, role);
            Creator checkCreator = _adapter.CheckForCreator(creator.Name);
            if (checkCreator != null)
            {
                creator = checkCreator;
            }
            else
            {
                creator = _adapter.CreateCreator(creator);
            }

            ComicBookCreator comicCreator = new ComicBookCreator(comicBookId, creator.Id);
            comicCreator = _adapter.CreateComicBookCreator(comicCreator);

            return RedirectToAction("Detail/" + creator.Id);
        }
        public ActionResult Detail(int id)
        {
            CreatorDetailVM bucket = new CreatorDetailVM();
            bucket.Creator = _adapter.GetCreator(id);
            bucket.Comics = _adapter.GetComicsForCreator(bucket.Creator);

            return View(bucket);
        }

        public ActionResult Edit(int id)
        {
            CreatorDetailVM bucket = new CreatorDetailVM();
            bucket.Creator = _adapter.GetCreator(id);
            bucket.Comics = _adapter.GetComicsForCreator(bucket.Creator);

            return View(bucket);
        }
        [HttpPost]
        public ActionResult Edit(Creator creator)
        {
            creator = _adapter.UpdateCreator(creator);
            return RedirectToAction("Detail/" + creator.Id);
        }
        public ActionResult Delete(int id)
        {
            Creator creator = _adapter.DeleteCreator(id);
            return RedirectToAction("Index");
        }

        public ActionResult RemoveCreatorLink(int comicBookId, int creatorId)
        {
            ComicBook comic = _adapter.GetComicBook(comicBookId);
            Creator creator = _adapter.GetCreator(creatorId);
            _adapter.BreakComicBookCreatorLink(comic, creator);
            return RedirectToAction("Detail/" + creator.Id);
        }
    }
}